package com.itic.mall.system.tixian.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.Constants;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.UserSessionHelper;
import com.itic.mall.system.expendlog.persistence.dao.ExpendLogDao;
import com.itic.mall.system.expendlog.persistence.model.ExpendLog;
import com.itic.mall.system.marketuser.persistence.dao.MarketUserDao;
import com.itic.mall.system.marketuser.persistence.model.MarketUser;
import com.itic.mall.system.statis.persistence.dao.StatisDao;
import com.itic.mall.system.statis.persistence.model.Statis;
import com.itic.mall.system.tixian.persistence.dao.TixianDao;
import com.itic.mall.system.tixian.persistence.model.Tixian;
import com.itic.mall.system.tixian.persistence.model.TixianExample;


@Service
@Transactional
public class TixianService
     extends CrudService<TixianDao,Tixian,TixianExample> {
    @Autowired
    private TixianDao tixianDao;
    @Autowired
    private ExpendLogDao expendLogDao;
    @Autowired
    private StatisDao statisDao;
    @Autowired
    private MarketUserDao marketUserDao;

	public String fafang(Tixian temp) {
		 //添加支出日志
        ExpendLog log = new ExpendLog();
       
        log.setId(IdGen.uuid());
        log.setIsEffective(DBConst.TRUE);
        log.setCreateTime(new Date());
        log.setUpdateTime(new Date());
        log.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper
                .get(Constants.USER_ID) : "unknown");
        log.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper
                .get(Constants.USER_ORG) : "unknown");
        
        log.setMoney(temp.getMoney());
        log.setType("微信");
        log.setNote("用户提现申请发放");
        log.setWxOrderNum("");
        
        Statis lastData = statisDao.getLastData();
        String lastMoney = "0";
        String wxMoney = "0";
        
        //平台统计信息
        Statis statis = new Statis();
        
        statis.setId(IdGen.uuid());
        statis.setIsEffective(DBConst.TRUE);
        statis.setCreateTime(new Date());
        statis.setUpdateTime(new Date());
        statis.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper
                .get(Constants.USER_ID) : "unknown");
        statis.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper
                .get(Constants.USER_ORG) : "unknown");
        
        if( null != lastData ){
        	if( StringHelper.isNotBlank(lastData.getTotalMoney())){
        		lastMoney = lastData.getTotalMoney();
        	}
        	
        	if( StringHelper.isNotBlank(lastData.getTotalWxMoney())){
        		lastMoney = lastData.getTotalWxMoney();
        	}
        	
        }
        lastMoney = NumberStringHelper.subtractString(lastMoney, temp.getMoney());
        statis.setTotalMoney(lastMoney);
        
        wxMoney = NumberStringHelper.subtractString(wxMoney, temp.getMoney());
        statis.setTotalWxMoney(wxMoney);
       
        tixianDao.updateByPrimaryKeySelective(temp);
        expendLogDao.insert(log);
        statisDao.insert(statis);
        
        
		return SUCCESS;
	}


	public String falseee(Tixian temp) {
		
		MarketUser user = marketUserDao.selectByPrimaryKey(temp.getUserId());
		String money = "0";
		if( StringHelper.isNotBlank(user.getMoney()) ){
			money = user.getMoney();
		}
		money = NumberStringHelper.addString(money, temp.getMoney());
		//如果有手续费
		String feeMoney = temp.getFeeMoney() + "";
		money = NumberStringHelper.addString(money, feeMoney);
		user.setMoney(money);
		
		marketUserDao.updateByPrimaryKeySelective(user);
		tixianDao.updateByPrimaryKeySelective(temp);
		return SUCCESS;
	}


}